//
//  HMPlaceHoldTextView.swift
//  WymanWeibo
//
//  Created by wyman on 16/4/19.
//  Copyright © 2016年 wyman. All rights reserved.
//

import UIKit

class HMPlaceHoldTextView: UITextView {

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var placeholder: String? {
        didSet{
        placeholderLabel.text = placeholder
        }
    
    }
    
    override var text: String!{
        didSet{
            placeholderLabel.hidden = self.hasText()
        }
    
    }
    
    override var font: UIFont? {
        didSet{
        placeholderLabel.font = font
        }
    }
   
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        
        prepareUI()
//        self.text = "分享新鲜事..."
        //发送通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("didChangtext"), name: UITextViewTextDidChangeNotification, object: nil)
        
    }
    
    deinit{
    NSNotificationCenter.defaultCenter().removeObserver(self)
    }

    //监听值得改变
    @objc private func didChangtext(){
    
//        HMPrint("值改变了,")
        placeholderLabel.hidden = hasText()
    
    }
    
    private func prepareUI(){
    
        addSubview(placeholderLabel)
        
        placeholderLabel.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: placeholderLabel, attribute: .Leading, relatedBy: .Equal, toItem: self, attribute: .Leading, multiplier: 1, constant: 8))
    
    
        addConstraint(NSLayoutConstraint(item: placeholderLabel, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1, constant: 8))
    }

    
    //懒加载
    private lazy var placeholderLabel: UILabel = {
    
    //添加占位Label
        let label = UILabel()
        label.font = self.font
        label.textColor = UIColor.lightGrayColor()
        
//        label.text = "分享新鲜事..."
        label.sizeToFit()
        
        return label
    
    
    
    }()
    
}







